package org.edu.modules.stu.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.edu.common.api.vo.Result;
import org.edu.common.constant.RoleConstant;
import org.edu.modules.stu.base.entity.VeBaseStudent;
import org.edu.modules.stu.base.mapper.VeBaseStudentMapper;
import org.edu.modules.stu.common.entity.VeBaseAppUser;
import org.edu.modules.stu.common.entity.VeBaseBanji;
import org.edu.modules.stu.common.entity.VeBaseTeacher;
import org.edu.modules.stu.common.mapper.VeBaseAppUserMapper;
import org.edu.modules.stu.common.mapper.VeBaseBanjiMapper;
import org.edu.modules.stu.common.mapper.VeBaseTeacherMapper;
import org.edu.modules.stu.common.service.VeBaseTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class VeBaseTeacherServiceImpl extends ServiceImpl<VeBaseTeacherMapper, VeBaseTeacher> implements VeBaseTeacherService {

    @Autowired
    private VeBaseTeacherMapper veBaseTeacherMapper;

    @Autowired
    private VeBaseAppUserMapper appUserMapper;

    @Autowired
    private VeBaseBanjiMapper veBaseBanjiMapper;

    @Autowired
    private VeBaseStudentMapper veBaseStudentMapper;

    @Override
    public VeBaseTeacher getByUserId(String userId) {
        return veBaseTeacherMapper.getByUserId(userId);
    }

    @Override
    public List<VeBaseStudent> getBySysUserId(String id) {
        //根据用户id查用户信息
        VeBaseAppUser appUser = appUserMapper.getVeBaseAppUserById(id);
        VeBaseTeacher baseTeacher = veBaseTeacherMapper.getByUserId(appUser.getUserId());
        if (baseTeacher != null) {
            //查班级
            List<VeBaseBanji> veBaseBanjiList = veBaseBanjiMapper.listByTeacherId(baseTeacher.getUserId());
            if (veBaseBanjiList != null && veBaseBanjiList.size() > 0) {
                //该教师是班主任，才查学生
                StringBuilder sb = new StringBuilder();
                for (VeBaseBanji baseBanji : veBaseBanjiList) {
                    //遍历班级，获取班级id
                    sb.append(baseBanji.getId()).append(",");  // 添加分隔符
                }
                // 去除末尾多余的分隔符
                String baseBanjiIds = sb.length() > 0 ? sb.deleteCharAt(sb.length() - 1).toString() : "";
                String[] ids = baseBanjiIds.split(",");
                List<VeBaseStudent> veBaseStudentList = veBaseStudentMapper.listByBanjinIds(ids);
                return veBaseStudentList;
            }
        }
        return null;
    }

    @Override
    public List<Integer> getStudentIdsListBySysUserId(String userId) {
        //获取角色
        List<Map<String, Object>> roleList = appUserMapper.getBaseSysRoleList(userId);
        VeBaseTeacher baseTeacher = veBaseTeacherMapper.getByUserId(userId);
        List<Integer> studentIdsList = new ArrayList<>();
        if (baseTeacher != null) {
            for (Map<String, Object> role : roleList) {
                if (role.get("code").equals(RoleConstant.SYS_ROLE_BZR)) {
                    //是班主任
                    //查班级
                    List<VeBaseBanji> veBaseBanjiList = veBaseBanjiMapper.listByTeacherId(baseTeacher.getUserId());
                    //根据班级查学生
                    if (veBaseBanjiList != null && veBaseBanjiList.size() > 0) {
                        //该教师是班主任，才查学生
                        StringBuilder sb = new StringBuilder();
                        for (VeBaseBanji baseBanji : veBaseBanjiList) {
                            //遍历班级，获取班级id
                            sb.append(baseBanji.getId()).append(",");  // 添加分隔符
                        }
                        // 去除末尾多余的分隔符
                        String baseBanjiIds = sb.length() > 0 ? sb.deleteCharAt(sb.length() - 1).toString() : "";
                        String[] ids = baseBanjiIds.split(",");
                        List<VeBaseStudent> studentList = veBaseStudentMapper.listByBanjinIds(ids);
                        if (studentList != null && studentList.size() > 0) {
                            studentIdsList = studentList.stream().map(veBaseStudent -> veBaseStudent.getId()).collect(Collectors.toList());
                            return studentIdsList;
                        } else {
                            //如果为空的话，学生id为0，即查不到数据
                            studentIdsList.add(0);
                            return studentIdsList;
                        }
                    } else {
                        //如果为空的话，学生id为0，即查不到数据
                        studentIdsList.add(0);
                        return studentIdsList;
                    }
                }
            }
            if (studentIdsList.size() == 0) {
                //如果为空的话，学生id为0，即查不到数据
                studentIdsList.add(0);
                return studentIdsList;
            }
        }
        return studentIdsList;
    }

    @Override
    public List<Integer> getBjIdsListBySysUserId(String userId) {
        //获取角色
        List<Map<String, Object>> roleList = appUserMapper.getBaseSysRoleList(userId);
        VeBaseTeacher baseTeacher = veBaseTeacherMapper.getByUserId(userId);
        List<Integer> bjIdsList = new ArrayList<>();
        if (baseTeacher != null) {
            for (Map<String, Object> role : roleList) {
                if (role.get("code").equals(RoleConstant.SYS_ROLE_BZR)) {
                    //是班主任
                    //查班级
                    List<VeBaseBanji> veBaseBanjiList = veBaseBanjiMapper.listByTeacherId(baseTeacher.getUserId());
                    if (veBaseBanjiList != null && veBaseBanjiList.size() > 0) {
                        bjIdsList = veBaseBanjiList.stream().map(veBaseBanji -> veBaseBanji.getId()).collect(Collectors.toList());
                        return bjIdsList;
                    } else {
                        //如果为空的话，班级id为0，即查不到数据
                        bjIdsList.add(0);
                        return bjIdsList;
                    }
                }
            }
            if (bjIdsList.size() == 0) {
                //如果为空的话，班级id为0，即查不到数据
                bjIdsList.add(0);
                return bjIdsList;
            }
        }
        return bjIdsList;
    }

    @Override
    public List<Integer> listGraduationByTeacherId(String userId) {
        //获取角色
        List<Map<String, Object>> roleList = appUserMapper.getBaseSysRoleList(userId);
        VeBaseTeacher baseTeacher = veBaseTeacherMapper.getByUserId(userId);
        List<Integer> bjIdsList = new ArrayList<>();
        if (baseTeacher != null) {
            for (Map<String, Object> role : roleList) {
                if (role.get("code").equals(RoleConstant.SYS_ROLE_BZR)) {
                    //是班主任
                    //查班级
                    List<VeBaseBanji> veBaseBanjiList = veBaseBanjiMapper.listGraduationByTeacherId(baseTeacher.getUserId());
                    if (veBaseBanjiList != null && veBaseBanjiList.size() > 0) {
                        bjIdsList = veBaseBanjiList.stream().map(veBaseBanji -> veBaseBanji.getId()).collect(Collectors.toList());
                        return bjIdsList;
                    } else {
                        //如果为空的话，学生id为0，即查不到数据
                        bjIdsList.add(0);
                        return bjIdsList;
                    }
                }
            }
            if (bjIdsList.size() == 0) {
                //如果为空的话，班级id为0，即查不到数据
                bjIdsList.add(0);
                return bjIdsList;
            }
        }
        return bjIdsList;
    }

}
